<template>
  <Card>
    <Form ref="searchForm" :label-width="90" :model="searchForm" inline>
      <Form-item label="车辆线路：" prop="line">
        <Select
          v-model="searchForm.line"
          clearable
          filterable
          placeholder="选择线路"
          style="width:200px;"
        >
          <Option value="-1">全部</Option>
          <Option v-for="item in lines" :key="item.value" :value="item.value">{{ item.label }}</Option>
        </Select>
      </Form-item>
      <Form-item label="日期：" prop="date_time">
        <DatePicker
          v-model="searchForm.start_date_time"
          placeholder="请选择开始日期"
          style="width: 200px"
          type="date"
        ></DatePicker>
        <DatePicker
          v-model="searchForm.end_date_time"
          placeholder="请选择结束日期"
          style="width: 200px"
          type="date"
        ></DatePicker>
      </Form-item>
      <Form-item style="margin-left:-90px;">
        <Button icon="md-search" type="primary" @click="handleSearch">搜索</Button>
        <Button @click="handleReset">重置</Button>
      </Form-item>
    </Form>
    <Button
      :disabled="exportBtnDisable"
      icon="md-cloud-download"
      style="margin-bottom:15px"
      type="primary"
      @click="exportKmStati"
    >导出
    </Button>
    <Table
      :columns="column"
      :data="data"
      :loading="loading"
      border
      size="small"
      stripe></Table>
    <Row class="page" justify="end" type="flex">
      <Page
        :current="searchForm.pageNumber"
        :page-size="searchForm.pageSize"
        :total="total"
        show-total
        @on-change="changePage"
      />
    </Row>
  </Card>
</template>
<script>
import {kmStatiList} from '../../api/chargeStati'

export default {
  data() {
    return {
      searchForm: {
        line: '-1',
        start_date_time: '',
        end_date_time: '',
        pageNumber: 1, // 当前页数
        pageSize: 10 // 页面大小
      },
      loading: false,
      data: [],
      modal: false,
      importLoading: false,
      exportBtnDisable: false,
      total: 0,
      column: [
        {
          type: 'index',
          width: 60,
          align: 'center'
        },
        {title: '日期', key: 'date', align: 'center', width: 200},
        {title: '线路', key: 'line', align: 'center', width: 200},
        {
          title: '车牌号',
          key: 'license_plate_num',
          align: 'center',
          width: 200
        },
        {title: '总里程', key: 'total_km', align: 'center', width: 200}
      ],
      lines: [],
      materials: [],
      date_text: ''
    }
  },
  watch: {},
  mounted() {
    // this.init();
    this.getLinesArray()
  },
  methods: {
    async getLinesArray() {
      this.lines = await this.getLines()
    },
    changePage(v) {
      this.searchForm.pageNumber = v
      this.getList()
    },
    //导出
    exportKmStati() {
      const line = this.searchForm.line
      const start_date_time = this.searchForm.start_date_time
      const end_date_time = this.searchForm.end_date_time
      const material = this.searchForm.material
      let start_date = ''
      let end_date = ''
      if (start_date_time) {
        const start_d = new Date(start_date_time)
        start_date =
          start_d.getFullYear() +
          '-' +
          (start_d.getMonth() + 1) +
          '-' +
          start_d.getDate() +
          ' ' +
          start_d.getHours() +
          ':' +
          start_d.getMinutes() +
          ':' +
          start_d.getSeconds()
      }
      if (end_date_time) {
        const end_d = new Date(end_date_time)
        end_date =
          end_d.getFullYear() +
          '-' +
          (end_d.getMonth() + 1) +
          '-' +
          end_d.getDate() +
          ' ' +
          end_d.getHours() +
          ':' +
          end_d.getMinutes() +
          ':' +
          end_d.getSeconds()
      }
      window.location.href =
        '/api/exportKmStati?line=' +
        line +
        '&start_date_time=' +
        start_date +
        '&end_date_time=' +
        end_date
    },
    getList() {
      this.loading = true
      kmStatiList(this.searchForm).then(res => {
        this.loading = false
        if (res.data) {
          this.data = res.data.result
          this.total = res.data.count
        }
      })
    },
    handleReset() {
      this.$refs.searchForm.resetFields()
      this.searchForm.pageNumber = 1
      this.searchForm.pageSize = 10
      this.getList()
    },
    handleSearch() {
      this.searchForm.pageNumber = 1
      this.searchForm.pageSize = 10
      this.getList()
    },
    init() {
      this.getList()
    }
  }
}
</script>
